package com.cskaoyan.service;

import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.bean.Category;
import com.cskaoyan.model.bean.CategoryExample;
import com.cskaoyan.model.vo.index.WXCatalogVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：Chunk
 * @Description:
 * @Date Created in 22:51 2021/4/13
 */
@Service
public class CatalogServiceImpl implements  CatalogService{
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    UserMapper userMapper;

//    Integer userId;
//    {
//        String username = (String) SecurityUtils.getSubject().getPrincipal();
//        userId = userMapper.selectIdByUsername(username);
//    }
    @Override
    public WXCatalogVo catalogIndex() {
        WXCatalogVo wxCatalogVo = new WXCatalogVo();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        //给CategoryList赋值
        wxCatalogVo.setCategoryList(categories);
        Category category = categoryMapper.selectByPrimaryKey(1005000);
        //给CurrentCategory赋值
        wxCatalogVo.setCurrentCategory(category);
        criteria.andPidEqualTo(1005000);
        List<Category> currentSubCategory = categoryMapper.selectByExample(categoryExample);
        //给CurrentSubCategory赋值
        wxCatalogVo.setCurrentSubCategory(currentSubCategory);
        return wxCatalogVo;
    }

    @Override
    public WXCatalogVo currentCategory(Integer id) {
        WXCatalogVo wxCatalogVo = new WXCatalogVo();
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(id);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        //给CurrentSubCategory赋值
        wxCatalogVo.setCurrentSubCategory(categories);
        Category category = categoryMapper.selectByPrimaryKey(id);
        //给CurrentCategory赋值
        wxCatalogVo.setCurrentCategory(category);
        return wxCatalogVo;
    }
}
